https://map-rfun.library.duke.edu/01_georeference.html
# install.packages("RPostgreSQL")
# install.packages("compare")
# install.packages("igraph")
# install.packages("rworldmap")
# install.packages("maps")
# install.packages("ggplot2")
# install.packages("tidyverse")
# install.packages("sf")
# install.packages("mapview")
# install.packages("dplyr")
library("RPostgreSQL")
Loading required package: DBI
library("compare")
Attaching package: ‘compare’
The following object is masked from ‘package:base’:
isTRUE
library("igraph")
Attaching package: ‘igraph’
The following object is masked from ‘package:compare’:
compare
The following objects are masked from ‘package:stats’:
decompose, spectrum
The following object is masked from ‘package:base’:
union
library("maps")
library("ggplot2")
library("rworldmap")
Loading required package: sp
### Welcome to rworldmap ###
For a short introduction type : vignette('rworldmap')
library("tidyverse")
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ tibble 3.1.6 ✓ dplyr 1.0.9
✓ tidyr 1.2.0 ✓ stringr 1.4.0
✓ readr 2.1.2 ✓ forcats 0.5.1
✓ purrr 0.3.4
── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
x purrr::compose() masks igraph::compose()
x tidyr::crossing() masks igraph::crossing()
x dplyr::filter() masks stats::filter()
x dplyr::groups() masks igraph::groups()
x dplyr::lag() masks stats::lag()
x purrr::map() masks maps::map()
x purrr::simplify() masks igraph::simplify()
library("sf")
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.0
library("mapview")
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
library("dplyr")
# Load the PostgreSQL driver, create a connection to the postgres database, and employ it.
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "mycodb", host = "localhost", port = 5432, user = "ruser", password = "ruser")
sql_string <- paste("SELECT * FROM mycodb", sep="")
MycoDB <- data.frame(dbGetQuery(con, sql_string))
#Set the working directory for files.
FileDirectory <- paste("/home/daiten/Programming/R/Projects/MycoDB/Graphics/", sep="")
Let’s map it all.
# get map
worldmap <- getMap(resolution = "coarse")
# plot world map
plot(worldmap, col = "lightgrey", fill = T, border = "darkgray", xlim = c(-180, 180), ylim = c(-90, 90), bg = "aliceblue", asp = 1, wrap=c(-180,180))
map()
starbucks <- read.csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv")
starbucksNC <- starbucks
starbucksNC
mapview(starbucksNC, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE)